package view.visualizations;

import java.util.Date;

import prefuse.Constants;
import prefuse.action.ActionList;
import prefuse.action.RepaintAction;
import prefuse.action.layout.AxisLabelLayout;
import prefuse.action.layout.AxisLayout;
import prefuse.data.expression.AndPredicate;
import prefuse.data.expression.ColumnExpression;
import prefuse.data.expression.ComparisonPredicate;
import prefuse.data.expression.ObjectLiteral;
import prefuse.data.expression.Predicate;
import prefuse.data.query.RangeQueryBinding;
import prefuse.render.AbstractShapeRenderer;
import prefuse.render.AxisRenderer;
import prefuse.render.Renderer;
import prefuse.render.RendererFactory;
import prefuse.render.ShapeRenderer;
import prefuse.visual.VisualItem;
import prefuse.visual.expression.VisiblePredicate;
import view.renderers.PointRenderer;
import view.renderers.TeamColorAction;
import data.DataManager;

public class MeetZoomVis extends XCVis {
	
	private Date date;
	private String meet;
	
	public MeetZoomVis(final Date date, final String meet) {
		super();
		this.date = date;
		this.meet = meet;
		this.removeGroup("data");
		Predicate datePredicate = new ComparisonPredicate(
				ComparisonPredicate.EQ, new ColumnExpression("Date"), new ObjectLiteral(date));
		Predicate meetPredicate = new ComparisonPredicate(
				ComparisonPredicate.EQ, new ColumnExpression("Meet"), new ObjectLiteral(meet));
		originalPredicate = new AndPredicate(datePredicate, meetPredicate);
		tupleSet = addTable("data", DataManager.getInstance().getTable(), originalPredicate);

		raceQuery = new RangeQueryBinding(tupleSet, "Race");
		
		xAxis = new AxisLayout("data", "Race", Constants.X_AXIS,
				VisiblePredicate.TRUE);
 
		yAxis = new AxisLayout("data", "Time", Constants.Y_AXIS,
				VisiblePredicate.TRUE);
		setupVis();
	}
	
	public Date getDate() {
		return date;
	}
	public String getMeet() {
		return meet;
	}
	
	@Override
	public void setupVis() {
		AxisLabelLayout x_labels = new AxisLabelLayout("xlab", xAxis);
		AxisLabelLayout y_labels = new AxisLabelLayout("ylab", yAxis);
		
		TeamColorAction color = new TeamColorAction("data", "School", Constants.NOMINAL, 
				VisualItem.STROKECOLOR, this);
		
		ActionList draw = new ActionList();
		draw.add(xAxis);
		draw.add(yAxis);
		draw.add(x_labels);
		draw.add(y_labels);
		draw.add(color);
		
		this.setRendererFactory(new RendererFactory() {
			AbstractShapeRenderer sr = new ShapeRenderer(7);
			PointRenderer pr = new PointRenderer(MeetZoomVis.this);
			Renderer arY = new AxisRenderer(Constants.FAR_LEFT,
					Constants.CENTER);
			Renderer arX = new AxisRenderer(Constants.CENTER,
					Constants.FAR_BOTTOM);
 
			public Renderer getRenderer(VisualItem item) {
				
				return item.isInGroup("ylab") ? arY
						: item.isInGroup("xlab") ? arX : pr;
			}
		});
		
		draw.add(new RepaintAction());
		this.putAction("draw", draw);
		this.run("draw");

		ActionList update = new ActionList();
		update.add(xAxis);
		update.add(yAxis);
		update.add(x_labels);
		update.add(y_labels);
		update.add(new RepaintAction());
		this.putAction("update", update);

		this.repaint();
	}

}
